using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Get, "PsSpecCmdletParameter", DefaultParameterSetName="ByName")]
    public class GetPsSpecCmdletParameterCommand : Cmdlet
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = "ByCmdlet")]
        public PsSpecCmdlet Cmdlet
        {
            get
            {
                return _cmdlet;
            }
            set
            {
                if (value != null)
                {
                    _cmdlet = value;
                    CmdletName = _cmdlet.Verb + "-" + _cmdlet.Noun;
                    ProjectName = _cmdlet.ProjectName;
                }
                else
                {
                    _cmdlet = null;
                    CmdletName = "*";
                    ProjectName = "*";
                }
            }
        }
        private PsSpecCmdlet _cmdlet;

        // ByName parameter Set
        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName="ByName")]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = "*";

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [ValidateLength(1, 500)]
        public String Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }
        private string _type = "*";

        [Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [AllowEmptyString]
        [AllowNull]
        public String Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
        private string _description = "*";

        [Parameter(Mandatory = false, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [ValidateLength(1, 50)]
        public String CmdletName
        {
            get
            {
                return _cmdletName;
            }
            set
            {
                _cmdletName = value;
            }
        }
        private string _cmdletName = "*";

        [Parameter(Mandatory = false, Position = 4, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [ValidateLength(1, 50)]
        public String ProjectName
        {
            get
            {
                return _projectName;
            }
            set
            {
                _projectName = value;
            }
        }
        private string _projectName = "*";

        [Parameter(Mandatory = false)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;


        protected override void ProcessRecord()
        {
            PsSpecCmdletService.PsSpecCmdletParameter[] parameters = null;

            PsSpecCmdletService.CmdletService service =
                new PsSpecCmdletService.CmdletService();
            service.Url = ServiceUri;

            service.UseDefaultCredentials = true;

            parameters = service.GetPsSpecCmdletParameter(
                PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(Name),
                PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(Type),
                PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(Description),
                PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(CmdletName),
                PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(ProjectName));

            foreach (PsSpecCmdletService.PsSpecCmdletParameter parameter in parameters)
            {
                WriteObject(new PsSpecCmdletParameter(
                        parameter.Id, parameter.ProjectName, parameter.CmdletName, parameter.Name, parameter.Type,
                        parameter.Description, parameter.Alias,
                        parameter.AllowGlobbing, parameter.AllowMultiple,
                        parameter.IsDynamic, parameter.AllowEmptyCollection, parameter.AllowEmptyString, parameter.AllowNull,
                        parameter.ValidateCount, parameter.ValidateNotNull, parameter.ValidateNotNullOrEmpty, 
                        parameter.ValidateRange, parameter.ValidateSet, parameter.ValidateLength, 
                        parameter.ValidatePattern, parameter.Note, parameter.DefaultValue,
                        parameter.Custom1, parameter.Custom2, parameter.Custom3, parameter.Custom4,
                        parameter.HelpDescription));
            }
        }
    }
}
